library(survey)
library(dplyr)
library(ggplot2)


# Chapter 5

### Figure 5.1
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_AFG_WON <- NA
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 6] <- 1
summary(factor(mil_conf.df$DOV_AFG_WON))

mil_conf.df$DOV_AFG_LOST <- NA
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 5] <- 1
summary(factor(mil_conf.df$DOV_AFG_LOST))

# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

summary(factor(df$P_ASSIGN1))
df$COVID_FAIL <- NA
df$COVID_FAIL[df$P_ASSIGN1 == 1] <- 0
df$COVID_FAIL[df$P_ASSIGN1 == 2] <- 1
summary(factor(df$COVID_FAIL))

df$COVID_SUCC <- NA
df$COVID_SUCC[df$P_ASSIGN1 == 1] <- 0
df$COVID_SUCC[df$P_ASSIGN1 == 3] <- 1
summary(factor(df$COVID_SUCC))

df$NGT <- NA
df$NGT[df$P_ASSIGN1 == 1] <- 0
df$NGT[df$P_ASSIGN1 == 4] <- 1
summary(factor(df$NGT))

df$NGC <- NA
df$NGC[df$P_ASSIGN1 == 1] <- 0
df$NGC[df$P_ASSIGN1 == 5] <- 1
summary(factor(df$NGC))


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

# Wave 1
# dov_assignment_a == 6 - won Afghanistan
AFW <- svyttest(Q8a~DOV_AFG_WON, w1_design)

# dov 5 - lost afghanistan
AFL <- svyttest(Q8a~DOV_AFG_LOST, w1_design)


# Wave 2
# P_ASSIGN1 == 2 COVID FAILURE
COVF <- svyttest(Q11_d~COVID_FAIL, w2_design)

# P_ASSIGN1 == 3 COVID SUCCESS
COVS <- svyttest(Q11_d~COVID_SUCC, w2_design)

# P_ASSIGN1 == 4 NG Traditional
NGTR <- svyttest(Q11_d~NGT, w2_design)

# P_ASSIGN1 == 5 NG Controversy
NGCO <- svyttest(Q11_d~NGC, w2_design)

pt.est <- c(AFW$estimate, 
            AFL$estimate, 
            COVS$estimate, 
            COVF$estimate,
            NGTR$estimate,
            NGCO$estimate)
ci.low <- c(AFW$conf.int[1], 
            AFL$conf.int[1], 
            COVS$conf.int[1], 
            COVF$conf.int[1],
            NGTR$conf.int[1],
            NGCO$conf.int[1])
ci.high <- c(AFW$conf.int[2], 
             AFL$conf.int[2], 
             COVS$conf.int[2], 
             COVF$conf.int[2],
             NGTR$conf.int[2],
             NGCO$conf.int[2])
plot.labs <- c("Won\nAfghanistan\nWar",
               "Lost\nAfghanistan\nWar",
               "COVID\nSuccess",
               "COVID\nFailure",
               "NG\nTraditional",
               "NG\nControversial")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()



##########################################################################



### Figure 5.2 - Party differences
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_AFG_WON <- NA
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 6] <- 1
summary(factor(mil_conf.df$DOV_AFG_WON))

mil_conf.df$DOV_AFG_LOST <- NA
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 5] <- 1
summary(factor(mil_conf.df$DOV_AFG_LOST))

# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

summary(factor(df$P_ASSIGN1))
df$COVID_FAIL <- NA
df$COVID_FAIL[df$P_ASSIGN1 == 1] <- 0
df$COVID_FAIL[df$P_ASSIGN1 == 2] <- 1
summary(factor(df$COVID_FAIL))

df$COVID_SUCC <- NA
df$COVID_SUCC[df$P_ASSIGN1 == 1] <- 0
df$COVID_SUCC[df$P_ASSIGN1 == 3] <- 1
summary(factor(df$COVID_SUCC))

df$NGT <- NA
df$NGT[df$P_ASSIGN1 == 1] <- 0
df$NGT[df$P_ASSIGN1 == 4] <- 1
summary(factor(df$NGT))

df$NGC <- NA
df$NGC[df$P_ASSIGN1 == 1] <- 0
df$NGC[df$P_ASSIGN1 == 5] <- 1
summary(factor(df$NGC))

df.11 <- df[!is.na(df$Q11_d),]
df.11 <- df.11[!is.na(df.11$party),]


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.11
  )

# Wave 1
# dov_assignment_a == 6 - won Afghanistan
AFW_d <- svyttest(Q8a~DOV_AFG_WON, w1_design[w1_design$variables$party==0])
AFW_i <- svyttest(Q8a~DOV_AFG_WON, w1_design[w1_design$variables$party==1])
AFW_r <- svyttest(Q8a~DOV_AFG_WON, w1_design[w1_design$variables$party==2])


# dov 5 - lost afghanistan
AFL_d <- svyttest(Q8a~DOV_AFG_LOST, w1_design[w1_design$variables$party==0])
AFL_i <- svyttest(Q8a~DOV_AFG_LOST, w1_design[w1_design$variables$party==1])
AFL_r <- svyttest(Q8a~DOV_AFG_LOST, w1_design[w1_design$variables$party==2])

# Wave 2
# P_ASSIGN1 == 2 COVID FAILURE
COVF_d <- svyttest(Q11_d~COVID_FAIL, w2_design[w2_design$variables$party==0])
COVF_i <- svyttest(Q11_d~COVID_FAIL, w2_design[w2_design$variables$party==1])
COVF_r <- svyttest(Q11_d~COVID_FAIL, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 3 COVID SUCCESS
COVS_d <- svyttest(Q11_d~COVID_SUCC, w2_design[w2_design$variables$party==0])
COVS_i <- svyttest(Q11_d~COVID_SUCC, w2_design[w2_design$variables$party==1])
COVS_r <- svyttest(Q11_d~COVID_SUCC, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 4 NG Traditional
NGTR_d <- svyttest(Q11_d~NGT, w2_design[w2_design$variables$party==0])
NGTR_i <- svyttest(Q11_d~NGT, w2_design[w2_design$variables$party==1])
NGTR_r <- svyttest(Q11_d~NGT, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 5 NG Controversy
NGCO_d <- svyttest(Q11_d~NGC, w2_design[w2_design$variables$party==0])
NGCO_i <- svyttest(Q11_d~NGC, w2_design[w2_design$variables$party==1])
NGCO_r <- svyttest(Q11_d~NGC, w2_design[w2_design$variables$party==2])

pt.est <- c(AFW_d$estimate, AFW_i$estimate, AFW_r$estimate, 
            AFL_d$estimate, AFL_i$estimate, AFL_r$estimate,
            COVS_d$estimate, COVS_i$estimate, COVS_r$estimate, 
            COVF_d$estimate, COVF_i$estimate, COVF_r$estimate,
            NGTR_d$estimate, NGTR_i$estimate, NGTR_r$estimate,
            NGCO_d$estimate, NGCO_i$estimate, NGCO_r$estimate)
ci.low <- c(AFW_d$conf.int[1], AFW_i$conf.int[1], AFW_r$conf.int[1], 
            AFL_d$conf.int[1], AFL_i$conf.int[1], AFL_r$conf.int[1],
            COVS_d$conf.int[1], COVS_i$conf.int[1], COVS_r$conf.int[1], 
            COVF_d$conf.int[1], COVF_i$conf.int[1], COVF_r$conf.int[1],
            NGTR_d$conf.int[1], NGTR_i$conf.int[1], NGTR_r$conf.int[1],
            NGCO_d$conf.int[1], NGCO_i$conf.int[1], NGCO_r$conf.int[1])
ci.high <- c(AFW_d$conf.int[2], AFW_i$conf.int[2], AFW_r$conf.int[2], 
             AFL_d$conf.int[2], AFL_i$conf.int[2], AFL_r$conf.int[2],
             COVS_d$conf.int[2], COVS_i$conf.int[2], COVS_r$conf.int[2], 
             COVF_d$conf.int[2], COVF_i$conf.int[2], COVF_r$conf.int[2],
             NGTR_d$conf.int[2], NGTR_i$conf.int[2], NGTR_r$conf.int[2],
             NGCO_d$conf.int[2], NGCO_i$conf.int[2], NGCO_r$conf.int[2])
plot.labs <- c(" ", "Won\nAfghanistan\nWar", "  ",
               "   ", "Lost\nAfghanistan\nWar", "    ",
               "     ", "COVID\nSuccess", "      ",
               "       ", "COVID\nFailure", "        ",
               "         ", "NG\nTraditional", "          ",
               "           ", "NG\nControversial", "            ")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c("blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red"))+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.35,.35)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()


##########################################################################


### Figure 5.3  
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_ETHICAL <- NA
mil_conf.df$DOV_ETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_ETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 7] <- 1
summary(factor(mil_conf.df$DOV_ETHICAL))

mil_conf.df$DOV_UNETHICAL <- NA
mil_conf.df$DOV_UNETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_UNETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 8] <- 1
summary(factor(mil_conf.df$DOV_UNETHICAL))


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )


# Wave 1
# dov_assignment_a == 7 - Ethical generals
ETH <- svyttest(Q8a~DOV_ETHICAL, w1_design)

# dov 8 - Unethical generals
UNETH <- svyttest(Q8a~DOV_UNETHICAL, w1_design)


pt.est <- c(ETH$estimate, UNETH$estimate)
ci.low <- c(ETH$conf.int[1], UNETH$conf.int[1])
ci.high <- c(ETH$conf.int[2], UNETH$conf.int[2])
plot.labs <- c("Ethical\nGenerals","Unethical\nGenerals")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.3,.1)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()



##########################################################################


### Figure 5.4 - Party differences
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_ETHICAL <- NA
mil_conf.df$DOV_ETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_ETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 7] <- 1
summary(factor(mil_conf.df$DOV_ETHICAL))

mil_conf.df$DOV_UNETHICAL <- NA
mil_conf.df$DOV_UNETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_UNETHICAL[mil_conf.df$DOV_ASSIGNMENT_A == 8] <- 1
summary(factor(mil_conf.df$DOV_UNETHICAL))



# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )


# Wave 1
# dov_assignment_a == 7 - Ethical generals
ETH_d <- svyttest(Q8a~DOV_ETHICAL, w1_design[w1_design$variables$party==0])
ETH_i <- svyttest(Q8a~DOV_ETHICAL, w1_design[w1_design$variables$party==1])
ETH_r <- svyttest(Q8a~DOV_ETHICAL, w1_design[w1_design$variables$party==2])

# dov 8 - Unethical generals
UNETH_d <- svyttest(Q8a~DOV_UNETHICAL, w1_design[w1_design$variables$party==0])
UNETH_i <- svyttest(Q8a~DOV_UNETHICAL, w1_design[w1_design$variables$party==1])
UNETH_r <- svyttest(Q8a~DOV_UNETHICAL, w1_design[w1_design$variables$party==2])


pt.est <- c(ETH_d$estimate, ETH_i$estimate, ETH_r$estimate,
            UNETH_d$estimate, UNETH_i$estimate, UNETH_r$estimate)
ci.low <- c(ETH_d$conf.int[1], ETH_i$conf.int[1], ETH_r$conf.int[1], 
            UNETH_d$conf.int[1], UNETH_i$conf.int[1], UNETH_r$conf.int[1])
ci.high <- c(ETH_d$conf.int[2], ETH_i$conf.int[2], ETH_r$conf.int[2],
             UNETH_d$conf.int[2], UNETH_i$conf.int[2], UNETH_r$conf.int[2])
plot.labs <- c(" ", "Ethical\nGenerals", "  ", "   ", "Unethical\nGenerals", "    ")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c("blue","gray50","red",
                                        "blue","gray50","red"))+
  geom_hline(yintercept = 0, color = "red")+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()




